package com.makeunion.gdlbsdemo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.LatLonSharePoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.share.ShareSearch;
import com.makeunion.gdlbsdemo.R;

/**
 * <p>write the description
 *
 * @author renjialiang
 * @version [版本号]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class ShareActivity extends AppCompatActivity
        implements ShareSearch.OnShareSearchListener {

    private LatLonPoint START = new LatLonPoint(39.989646, 116.480864);
    private LatLonPoint END = new LatLonPoint(39.983456, 116.3154950);
    private LatLonPoint POI_POINT = new LatLonPoint(39.968993, 116.423991);

    private MapView mapView;

    private AMap aMap;

    private ShareSearch shareSearch;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);

        mapView = (MapView) findViewById(R.id.map_view);
        mapView.onCreate(savedInstanceState); //此方法必须重写
        init();
    }

    private void init() {
        if (aMap == null) {
            aMap = mapView.getMap();
        }
        shareSearch = new ShareSearch(this.getApplicationContext());
        shareSearch.setOnShareSearchListener(this);
    }

    /**
     * 位置分享
     */
    public void locationShare(View view) {
        LatLonSharePoint point = new LatLonSharePoint(POI_POINT.getLatitude(),
                POI_POINT.getLongitude(), "方恒国际中心A座");
        shareSearch.searchLocationShareUrlAsyn(point);
    }

    /**
     * 路径分享
     */
    public void routeShare(View view) {
        ShareSearch.ShareFromAndTo fromAndTo = new ShareSearch.ShareFromAndTo(START, END);
        ShareSearch.ShareDrivingRouteQuery query = new ShareSearch.ShareDrivingRouteQuery(fromAndTo,
                ShareSearch.DrivingDefault);
        shareSearch.searchDrivingRouteShareUrlAsyn(query);
    }

    /**
     * poi分享
     */
    public void poiShare(View view) {
        // id, LatLngPoint, title, snippet
        PoiItem item = new PoiItem("B000A8455H", POI_POINT, "麦当劳(和平街北口店)", "北三环东路22号");
        shareSearch.searchPoiShareUrlAsyn(item);
    }

    /**
     * 导航分享
     */
    public void navigationShare(View view) {
        ShareSearch.ShareFromAndTo fromAndTo = new ShareSearch.ShareFromAndTo(START, END);
        ShareSearch.ShareNaviQuery query = new ShareSearch.ShareNaviQuery(fromAndTo,
                ShareSearch.NaviDefault);
        shareSearch.searchNaviShareUrlAsyn(query);
    }

    @Override
    public void onPoiShareUrlSearched(String url, int errorCode) {
        Log.e("TAG", "PoiShare url=" + url);
        Toast.makeText(this, url, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLocationShareUrlSearched(String url, int errorCode) {
        Log.e("TAG", "LocationShare url=" + url);
        Toast.makeText(this, url, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNaviShareUrlSearched(String url, int errorCode) {
        Log.e("TAG", "NavigationShare url=" + url);
        Toast.makeText(this, url, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onBusRouteShareUrlSearched(String url, int errorCode) {
        Log.e("TAG", "BusRouteShare url=" + url);
        Toast.makeText(this, url, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onWalkRouteShareUrlSearched(String url, int errorCode) {
        Log.e("TAG", "WalkRouteShare url=" + url);
        Toast.makeText(this, url, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDrivingRouteShareUrlSearched(String url, int errorCode) {
        Log.e("TAG", "DrivingRouteShare url=" + url);
        Toast.makeText(this, url, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
}
